package com.ydc.ln.tutorial.algorithm.work.zjh;


/**
 * 高效查找find(String str, char ch);
 * 
 * @author ydc
 * @date 下午4:42:48 2013-6-26
 */
public class Ch02 {
    public static void main(String[] args) {
        System.out.println(Ch02.find("123456", '7'));
    }

    public static int find(String str, char ch) {
        int pos = -1;
        char[] strs = str.toCharArray();
        int low = 0;
        int high = str.length();
        while (true) {
            int mid = (low + high) / 2;
            if (Math.abs(high - low) <= 1) {
                break;
            }
            if (strs[mid] == ch) {
                pos = mid;
                break;
            } else if (strs[mid] > ch) {
                high = mid;
            } else {
                low = mid;
            }
        }

        return pos;
    }
}
